package cn.stylefeng.guns.modular.ums.controller;

import cn.stylefeng.guns.base.enums.CommonStatus;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.cms.entity.AboutUs;
import cn.stylefeng.guns.modular.cms.wrapper.AdWrapper;
import cn.stylefeng.guns.modular.ums.entity.Member;
import cn.stylefeng.guns.modular.ums.model.params.MemberParam;
import cn.stylefeng.guns.modular.ums.service.MemberAccountService;
import cn.stylefeng.guns.modular.ums.service.MemberService;
import cn.stylefeng.guns.modular.ums.wrapper.MemberWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.reqres.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;


/**
 * 会员信息表控制器
 *
 * @author yzb
 * @Date 2019-11-07 16:03:07
 */
@Controller
@RequestMapping("/member")
public class MemberController extends BaseController {

    private String PREFIX = "/ums/member";

    @Autowired
    private MemberService memberService;

    /**
     * 跳转注册页面
     *
     * @author yzb
     * @Date 2019-11-07
     */
    @RequestMapping("/register")
    public String register() {
        return "/ums/register/index.html";
    }

    /**
     * 跳转注册页面
     *
     * @author yzb
     * @Date 2019-11-07
     */
    @RequestMapping("/appDownload")
    public String appDownload() {
        return "/ums/register/appdownload.html";
    }

    /**
     * 跳转到主页面
     *
     * @author yzb
     * @Date 2019-11-07
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/member.html";
    }


    /**
     * 新增页面
     *
     * @author yzb
     * @Date 2019-11-07
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/member_add.html";
    }

    /**
     * 编辑页面
     *
     * @author yzb
     * @Date 2019-11-07
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/member_edit.html";
    }

    /**
     * 新增接口
     *
     * @author yzb
     * @Date 2019-11-07
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(MemberParam memberParam) {
        this.memberService.add(memberParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author yzb
     * @Date 2019-11-07
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(MemberParam memberParam) {
        this.memberService.update(memberParam);
        return ResponseData.success();
    }


    /**
     * 审核会员认证信息
     * @param memberParam
     * @return
     */
    @RequestMapping("/veriDateMember")
    @ResponseBody
    public ResponseData veriDateMember(MemberParam memberParam) {
        memberParam.setStatus("IS_CERT");
        return  ResponseData.success(this.memberService.update(memberParam));
    }

    /**
     * 删除接口
     *
     * @author yzb
     * @Date 2019-11-07
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(MemberParam memberParam) {
        this.memberService.delete(memberParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author yzb
     * @Date 2019-11-07
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(MemberParam memberParam) {
        Member detail = this.memberService.getById(memberParam.getMemberId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author yzb
     * @Date 2019-11-07
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(MemberParam memberParam) {
        Page<Map<String, Object>> members = memberService.findPageMapBySpec(memberParam);
        Page wrapped = new MemberWrapper(members).wrap();
        return LayuiPageFactory.createPageInfo(wrapped);
    }


    /**
     * 修改状态
     *
     * @author yzb
     * @Date 2019-06-27
     */
    @ResponseBody
    @RequestMapping("/changeStatus")
    public ResponseData changeStatus(@RequestParam("id") String id,
                                     @RequestParam("status") Boolean status) {
        Member member = this.memberService.getById(id);
        if (member == null) {
            throw new RequestEmptyException();
        }
        if (status) {
            member.setStatus(CommonStatus.ENABLE.getCode());
        } else {
            member.setStatus(CommonStatus.DISABLE.getCode());
        }
        this.memberService.updateById(member);
        return new SuccessResponseData();
    }
}


